package com.tgpms.web.earlierStage.service.impl;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.DocSortMapper;
import com.tgpms.web.earlierStage.entity.DocSort;
import com.tgpms.web.earlierStage.service.EarlierDocSortService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EarlierDocSortServiceImpl implements EarlierDocSortService {

	@Autowired
	private DocSortMapper docSortMapper;

	@Override
	public Result addDocSort(DocSort docSort, Result result) {
		//主键形式 yyyyMMddHHmmss+UUID
		docSort.setSortId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		docSort.setRowstamp(docSort.getSortId());
		docSort.setInsertDate(new Date());
		int i = docSortMapper.insertSelective(docSort);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(docSort.getId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateDocSort(DocSort docSort, Result result) {
		docSort.setModifyDate(new Date());
		int i = docSortMapper.updateByPrimaryKeySelective(docSort);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteDocSort(String sortId, Result result) {
		int i = docSortMapper.deleteByPrimaryKey(sortId);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

}
